home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / C / KWTREE.ZIP / KWTREE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-26  |  4.0 KB  |  136 lines

  1. /*****************************************************************************
  2.  
  3.   Program: KWTREE.C
  4.   
  5.   Purpose: List all of the directories on current disk drive
  6.   
  7.   Date: 08-25-1993
  8.   
  9.   Author: Karl Weller [74620,2112]
  10.           (216)-397-9949
  11.           
  12. *****************************************************************************/
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <conio.h>
  18. #include <ctype.h>
  19. #include <dos.h>
  20. #include <io.h>
  21. #include <sys\types.h>
  22. #include <sys\stat.h>
  23.  
  24. char bars[] = "|/-\\";
  25. int addcount=0;
  26.  
  27. char *do_malloc(unsigned);
  28. void add_entry(char *name);
  29.  
  30. char *dirptr[8000];
  31. int dircnt=0;
  32. int dirpos=0;
  33.  
  34. int main(int argc, char *argv[],char *envp[])
  35. {
  36.     char filespec[400];
  37.     char *tmp;
  38.     register int i,j;
  39.     struct find_t find;
  40.     
  41.     
  42.     dirptr[0] = do_malloc(10);
  43.  
  44.   /* change to     "c:\\" for listing a specific drive */
  45.     strcpy(dirptr[0],"\\"); 
  46.  
  47.   /* Give credit where credit is due <g>! */ 
  48.     fprintf(stderr,"Karl's Directory Tree Lister v1.0\nThis program lists all the directories on the current drive\nWritten by: Karl Weller [74620,2112] 08-25-1993 (Press ESC to Exit)\n\n");
  49.     fprintf(stderr,"Reading....%c",bars[addcount++%4]);
  50.  
  51.   /*
  52.      This is the main loop, loop through each directory looking for additional
  53.      directories.   This would be a great program to use recursion, but I
  54.      don't have the guts!  < oh, well no glory >
  55.      
  56.      (The last statement will probably nag me until I rewrite the loop to be 
  57.       recursive)
  58.   */
  59.     while(dirpos<=dircnt) {
  60.         if (dircnt>7995) { /* only allocated 8000 pointers see declaration
  61.                               for dirptr */
  62.             printf("Too many directories!\n");
  63.             exit(0);
  64.         }
  65.         strcpy(filespec,dirptr[dirpos]); /* copy the base directory to the */
  66.         strcat(filespec,"*.*");          /* file spec. */
  67.         if (kbhit()) {
  68.             if (getch()==27) {
  69.                 fprintf(stderr,"***Escape Hit***\n");
  70.                 goto exit;
  71.             }
  72.         }
  73.         if( !_dos_findfirst( filespec, 0xffff, &find ) ) {
  74.             if (find.attrib & _A_SUBDIR) {
  75.                 add_entry(find.name); /* Found new dir, add to list */
  76.             }
  77.             while( !_dos_findnext( &find ) ) {
  78.                 if (find.attrib & _A_SUBDIR) {
  79.                     add_entry(find.name); /* Found new dir, add to list */
  80.                 }
  81.             }
  82.         }
  83.         ++dirpos;
  84.     }
  85.  
  86.     fprintf(stderr,"\nSorting...");
  87.     for(i=0;i<=dircnt-1;++i) {
  88.         for(j=i+1;j<=dircnt;++j) {
  89.             if (strcmp(dirptr[j],dirptr[i]) < 0) {
  90.                 tmp = dirptr[i];
  91.                 dirptr[i] = dirptr[j];
  92.                 dirptr[j] = tmp;
  93.             }
  94.         }
  95.     }
  96.  
  97.     fprintf(stderr,"\n");
  98.     for(i=0;i<=dircnt;++i) {          /* Display the tree ! */
  99.         printf("%s\n",dirptr[i]);
  100.         free(dirptr[i]);
  101.     }
  102. exit:
  103.     return(0);
  104. }
  105.  
  106. /*****************************************************************************
  107.  Malloc w/ error checking.
  108.  
  109.  Die if no memory available
  110. *****************************************************************************/
  111. char *do_malloc(unsigned size)
  112. {
  113.     char *ptr;
  114.     
  115.     ptr = (char *)malloc(size);
  116.     if (ptr == NULL) {
  117.         fprintf(stderr,"\nInsufficient memory for malloc\n");
  118.         exit(-1);
  119.     }
  120.     return(ptr);
  121. }
  122.  
  123. /*****************************************************************************
  124.  Add a directory to the list
  125. *****************************************************************************/
  126. void add_entry(char *name)
  127. {
  128.     fprintf(stderr,"%c%c",(char)8,bars[addcount++%4]);
  129.  
  130.     if (*name != '.') { /* if it's a dot listing skip */
  131.         ++dircnt;
  132.         dirptr[dircnt] = do_malloc(4+strlen(name)+strlen(dirptr[dirpos]));
  133.         sprintf(dirptr[dircnt],"%s%s\\",dirptr[dirpos],name);
  134.     }
  135. }
  136.